package com.jin.api.v1;

import com.jin.bo.PageCounter;
import com.jin.model.Spu;
import com.jin.service.SpuService;
import com.jin.util.CommonUtil;
import com.jin.vo.PagingDozer;
import com.jin.vo.SpuSimplifyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

//@RequestParam和@PathVariable这两者之间区别不大，主要是请求的URL不一样
//
//        用@RequestParam请求接口时,URL是:http://www.test.com/user/getUserById?userId=1
//
//        用@PathVariable请求接口时,URL是:http://www.test.com/user/getUserById/2


import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.jin.Exception.http.NotFoundException;
import com.jin.bo.PageCounter;
import com.jin.model.Spu;
import com.jin.service.SpuService;
import com.jin.util.CommonUtil;
import com.jin.vo.PagingDozer;
import com.jin.vo.SpuSimplifyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/spu")
@Validated
public class SpuController {
    @Autowired
    SpuService spuService;
    @GetMapping("/id/{id}/detail")
    public Spu getDetail(@PathVariable @Positive Long id ){
        Spu spu = spuService.getSpu(id);
        if (spu==null){
            throw new NotFoundException(30003);
        }
            return spu;
    }

    //用vo的形式返回一组简单的spu，多组就要用DozerBeanMapperBuilder了
    @GetMapping("/id/{id}/simplify")                            //message的错误信息从配置文件里面读取
    public SpuSimplifyVo getSimplifySpu(@PathVariable @Positive(message = "{id.positive}") Long id){
        Spu spu = spuService.getSpu(id);
        SpuSimplifyVo vo = new SpuSimplifyVo();
        BeanUtils.copyProperties(spu,vo);
        return vo;
    }


    //根据创建时间获取一组分页的spu
    //用vo的形式获取多组spu的数据
    @GetMapping("/latest")
    public PagingDozer<Spu,SpuSimplifyVo> getLatestSpuList(@RequestParam(defaultValue = "3") Integer start,
                                                @RequestParam(defaultValue = "3 ") Integer count){
        //参数的转换
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start,count);
        //分页操作
        Page<Spu> page = spuService.getLatestPagingSpu(pageCounter.getPage(), pageCounter.getSize());
//        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
//        List<SpuSimplifyVo> vos = new ArrayList<>();
//        //遍历spuList,s代表一个spu，跟增强for循环:前面的变量是一样的
//        spuList.forEach(s-> {
//            //复制属性，还可以解决属性里面有引用类型
//            SpuSimplifyVo vo = mapper.map(s,SpuSimplifyVo.class);
//            vos.add(vo);
//        });
//        return vos;
        return new PagingDozer<>(page,SpuSimplifyVo.class);
    }

    //通过分类id获取spu
    @GetMapping("/by/category/{id}")
    public PagingDozer<Spu,SpuSimplifyVo> getByCategoryId(@PathVariable(name = "id") Long id,
                                                          @RequestParam(name = "is_root",defaultValue = "false") Boolean isRoot,
                                                          @RequestParam(name = "start",defaultValue = "3")
                                                          Integer start,
                                                          @RequestParam(name = "count",defaultValue = "3")
                                                          Integer count){
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start,count);
        Page<Spu> page = spuService.getByCategory(id,isRoot,pageCounter.getPage(), pageCounter.getSize());
        return new PagingDozer<>(page,SpuSimplifyVo.class);
    }
}
